//index.js
//获取应用实例
const app = getApp();
var njzxw = require('../../../utils/njzxw.js');
var url = require('../../../utils/url.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:null,
    isUrl:false,
    data: {}
  },
  /**
     * 取消收藏
     */
  deleteCommon: function (e) {
    var res_id = njzxw.getDataVal(e, 'id');
    var $this = this;
    njzxw.alert({
      msg: '确定删除吗？',
      confirm: true,

      fun: function (a) {

        if (a) {
          njzxw.ajax({
            url: url.commeninfo.delete,
            data: {
              ids: [
                res_id
              ]
            },
            load: true,
            fun: function (a, b) {
              wx.hideLoading();
              if (a) {
                if (b.data.state == 1) {
                  njzxw.alert({
                    msg: '操作成功',
                    fun: function () {
                      $this.loadData($this);
                    }
                  });

                } else {
                  njzxw.alert({
                    error: true,
                    msg: '删除失败'
                  });
                }
              } else {
                njzxw.alert({
                  error: true,
                  msg: '操作失败'
                });
              }
            }
          });
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id:options.id
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var $this = this;
    njzxw.initFun(this);
    this.loadData(this);
  },
  loadData:function($this){
    njzxw.ajax({
      url: url.encyclopediaInfoFront.queryId,
      load: true,
      data: {
        id: $this.data.id
      },
      fun: function (a, b) {
        wx.hideLoading();
        if (a) {
          if (b.data.state == 1) {
            var data = b.data.data.list;
            var hf = b.data.data.hfList;
            if (data.hasOwnProperty('context')){
              data.context = njzxw.replaceAll(data.context,/<img/g,'<img style="max-width: 100%" ');
            }
            $this.setData({
              data: data,
              hf: hf,
              isUrl: data.is_link == 1
            });
            console.log("isUrlisUrl::" + $this.data.isUrl + "==" + data.is_link);
          } else {
            njzxw.alert({
              msg: '数据加载失败'
            });
          }
        } else {
          njzxw.alert({
            msg: '数据加载失败'
          });
        }
      }
    });
  },
  dianzhan: function (e) {
    var $this = this;
    var id = njzxw.getDataVal(e, 'id');
    var type = njzxw.getDataVal(e, 'type');
    njzxw.ajax({
      url: url.encyclopediaInfoFront.dianzhan,
      load: true,
      data: {
        id: id,
        type: type
      },
      fun: function (a, b) {
        wx.hideLoading();
        if (a) {
          if (b.data.state == 1) {
            wx.showToast({
              title: '点赞成功',
            })
            $this.loadData($this);
          } else {
            njzxw.alert({ msg: b.data.msg });
          }
        } else {
          njzxw.alert({ msg: '操作失败' });
        }
      }
    });
  },
  /**
   * 显示回复按钮
   */
  showAction: function (e) {
    var name = e.currentTarget.dataset.name;
    var param = njzxw.getDataVal(e, 'param');
    wx.showActionSheet({
      itemList: ["回复【" + name + "】"],
      success: function (res) {
        wx.navigateTo({
          url: '/pages/tangyouquan/huifu/huifu?' + param,
        })
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    });
    return false;
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})